package com.situ.springboot.controller;

import com.situ.springboot.pojo.Dept;
import com.situ.springboot.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private IDeptService deptService;

    // http://localhost:3306/dept/list
    @RequestMapping("/list")
    public String selectAll(Model model) {
        System.out.println("DeptController.selectAll");

        //查找数据库
        List<Dept> list = deptService.selectAll();
        model.addAttribute("list", list);
        //跳转到页面展示
        return "/dept/dept_list";
    }

    @RequestMapping("/deleteById")
    public String deleteById(Integer id) {
        deptService.deleteById(id);

        //删除完了之后，查找最新的数据
        return "redirect:/dept/list";
    }

    @RequestMapping("/toAdd")
    public String toAdd() {
        return "/dept/dept_add";
    }

    @RequestMapping("/add")
    public String add(Dept dept) {
        deptService.add(dept);

        //添加完了之后，查找最新的数据
        return "redirect:/dept/list";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(Integer id, Model model) {
        Dept dept = deptService.selectById(id);
        model.addAttribute("dept", dept);
        return "/dept/dept_update";
    }

    @RequestMapping("/update")
    public String update(Dept dept) {
        System.out.println("dept: " + dept);
        deptService.update(dept);

        //更新完了之后，查找最新的数据
        return "redirect:/dept/list";
    }
   
}
